package com.guli.produce.produce.service.impl;

import com.guli.produce.produce.dao.AttrGroupDao;
import com.guli.produce.produce.dao.BrandDao;
import com.guli.produce.produce.dao.CategoryDao;
import com.guli.produce.produce.entity.AttrGroupEntity;
import com.guli.produce.produce.entity.BrandEntity;
import com.guli.produce.produce.entity.CategoryEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.produce.produce.dao.CategoryBrandRelationDao;
import com.guli.produce.produce.entity.CategoryBrandRelationEntity;
import com.guli.produce.produce.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @SuppressWarnings("all")
    @Autowired
    private CategoryDao categoryDao;
    @SuppressWarnings("all")
    @Autowired
    private BrandDao brandDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<CategoryBrandRelationEntity> wapper = new QueryWrapper<>();
        String brandId = (String) params.get("brandId");
        wapper.eq("brand_id",Long.parseLong(brandId));
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                wapper
        );

        return new PageUtils(page);
    }

    @Override
    public boolean save(CategoryBrandRelationEntity entity) {
        //获取分组id和分类id
        Long catelogId = entity.getCatelogId();
        Long brandId = entity.getBrandId();

        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        BrandEntity attrGroupEntity = brandDao.selectById(brandId);

        entity.setBrandName(attrGroupEntity.getName());
        entity.setCatelogName(categoryEntity.getName());
        return super.save(entity);
    }
}